package org.luckyjourney.service.video.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.luckyjourney.entity.video.Video;
import org.luckyjourney.entity.video.VideoStar;
import org.luckyjourney.entity.video.vo.VideoStartCountVO;
import org.luckyjourney.mapper.video.VideoStarMapper;
import org.luckyjourney.service.video.VideoService;
import org.luckyjourney.service.video.VideoStarService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xhy
 * @since 2023-10-24
 */
@Service
public class VideoStarServiceImpl extends ServiceImpl<VideoStarMapper, VideoStar> implements VideoStarService {

    @Autowired
    VideoStarMapper videoStarMapper;

    @Autowired
    private VideoService videoService;

    @Override
    public VideoStartCountVO starVideo(VideoStar videoStar) {

        LambdaQueryWrapper<VideoStar> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VideoStar::getVideoId, videoStar.getVideoId());
        queryWrapper.eq(VideoStar::getUserId, videoStar.getUserId());
        queryWrapper.eq(VideoStar::getModuleId, videoStar.getModuleId());
        VideoStar videoStar1 = videoStarMapper.selectOne(queryWrapper);
        if (ObjectUtil.isEmpty(videoStar1)) {
            videoStarMapper.insert(videoStar);
            VideoStartCountVO videoStartCountVO = getVideoStartCountVO(videoStar);
            videoStartCountVO.setABoolean(true);
            return videoStartCountVO;
        } else {
            videoStarMapper.delete(queryWrapper);
            VideoStartCountVO videoStartCountVO = getVideoStartCountVO(videoStar);
            videoStartCountVO.setABoolean(false);
            return videoStartCountVO;
        }
    }

    private VideoStartCountVO getVideoStartCountVO(VideoStar videoStar) {
        //视频
        Video video = videoService.getById(videoStar.getVideoId());
        LambdaQueryWrapper<VideoStar> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VideoStar::getVideoId, videoStar.getVideoId());
        Integer selectCount = videoStarMapper.selectCount(wrapper);
        Long startCounts = Long.valueOf(selectCount);
        //修改视频中点赞的数量
        video.setStartCount(startCounts);
        videoService.saveOrUpdate(video);
        VideoStartCountVO videoStartCountVO = new VideoStartCountVO();
        videoStartCountVO.setStartCount(selectCount);
        return videoStartCountVO;
    }


    @Override
    public List<Long> getStarUserIds(Long videoId) {
        return this.list(new LambdaQueryWrapper<VideoStar>().eq(VideoStar::getVideoId, videoId)).stream().map(VideoStar::getUserId).collect(Collectors.toList());
    }

    @Override
    public Boolean starState(Long videoId, Long userId) {

        if (userId == null) return false;

        return this.count(new LambdaQueryWrapper<VideoStar>().eq(VideoStar::getVideoId, videoId).eq(VideoStar::getUserId, userId)) == 1;
    }
}
